import entity.Employee;

import java.util.Optional;

public class UseOptional2 {
    public static void main(String[] args) {
        Employee employee = null;
        test(employee);
        Employee employee2 = null;
        test2(employee2);

    }

    private static void test(Employee employee){
//        Employee employee = new Employee();
//        Optional<Employee> employeeOptional = Optional.of(employee);
//        System.out.println(employeeOptional.get().getName());

        //get方法 用来返回Optional中的值，如果为null，则会抛出异常：NoSuchElementException、
        Optional<Employee> employeeOptionalNullAble = Optional.ofNullable(employee);
        Employee employee1 = employeeOptionalNullAble.orElse(new Employee());
        System.out.println(employee1.getName());
//        System.out.println(employeeOptionalNullAble.get().getName());

    }

    private static void test2(Employee employee){
        Optional<Employee> employeeOp = Optional.ofNullable(employee);
        Optional<Employee> employee2 = Optional.of(employeeOp.orElse(new Employee()));
        employee2.ifPresent(employee1 -> {
            employee1.setGender("不知道");
        });
        System.out.println(employee2.get().getGender());
    }

}
